package com.zz.shop.service.impl;

import com.zz.shop.common.Const;
import com.zz.shop.common.ResponseResult;
import com.zz.shop.dao.UserDao;
import com.zz.shop.dao.impl.UserDaoImpl;
import com.zz.shop.entity.User;
import com.zz.shop.service.UserService;
import com.zz.shop.util.*;

import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    private UserDao ud=new UserDaoImpl();
    @Override
    public ResponseResult findUserByName(String username) {
        System.out.println(username);
        try {
            if (StringUtils.isNotEmpty(username)) {
                User user=ud.findUserByName(username);
                if (user != null) {
                    return ResponseResult.createSuccess("用户名已存在");
                }
            }else {
                return ResponseResult.createSuccess("用户名不能未空");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("可以");
        return ResponseResult.createFail("用户名可用");
    }

    @Override
    public ResponseResult reg(User u) {
        try {
            if (u != null) {
                u.setUrole(Const.UserRole.USER);
                u.setUstatus(Const.UserRole.NOT_ACTIVE);
                u.setUcode(RandomUtils.createActive());
                u.setUpassword(MD5Utils.md5(u.getUpassword()));
                if (ud.insertUser(u)>0) {
                    new Thread(()->EmailUtils.sendEmail(u)).start();
                    return ResponseResult.createSuccess("注册成功");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("注册失败");
    }

    @Override
    public ResponseResult doLogin(String username, String password) {
        try {
            if (StringUtils.isNotEmpty(username,password)) {
                User user =  ud.getUserByUsername(username);
                // 数据库获取的密码是被加密过的密码
                // 把用户输入的密码先进行加密MD5,在进行比较
                String md5=MD5Utils.md5(password);
                if (user!=null&&user.getUpassword().equals(md5)) {
                    // 判断用户是否激活
                    if (user.getUstatus()== Const.UserRole.ACTIVE) {
                        // 登录成功
                        return ResponseResult.createSuccess(user);
                    }else{
                        // 请先激活
                        return ResponseResult.createFail("账户未激活,请先激活");
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("用户或密码有误,请重新再试");
    }

    @Override
    public ResponseResult sendMessage(String phone) {
        if (StringUtils.isNotEmpty(phone)) {
            String send = SendMessage.send(phone);
            System.out.println(send+"1111");
            return ResponseResult.createSuccess(send);
        }
        return ResponseResult.createFail();
    }

    @Override
    public ResponseResult activate(String e, String c) {
        try {
            //1 判断是否为Null
            if (!StringUtils.isNotEmpty(e,c)) {
                // 有null
                return ResponseResult.createFail("激活失败,邮箱或激活码有误,请重新再试或联系管理员");
            }
            //2 解码
            String email = Base64Utils.decode(e);
            String code = Base64Utils.decode(c);
            //3 判断有没有这个对应的数据 查询数据 email + code 查询
            User user = ud.getUserByEmailAndCode(email,code);
            //4 判断数据是否为null
            if (user==null) {
                return ResponseResult.createFail("没有此账户,无法激活");
            }
            //5 判断用户是否已激活
            if (user.getUstatus()== Const.UserRole.ACTIVE) {
                //用户已经有激活了
                return ResponseResult.createFail("账户已激活,无需重复操作,请直接登录");
            }
            //6 进行激活
            if (ud.activate(user.getUid())>0) {
                return ResponseResult.createSuccess("激活成功,请进入首页登录");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return ResponseResult.createFail("激活失败,请不要再试");
    }
}
